<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">

      <el-form-item label="名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入优惠券名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="券类型" prop="pointsType">
        <el-input
          v-model="queryParams.pointsType"
          placeholder=""
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否有效" prop="pointsValue">
        <el-input
          v-model="queryParams.pointsType"
          placeholder=""
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="发放方式" prop="sendType">
        <el-input
          v-model="queryParams.sendType"
          placeholder=""
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>


      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd()"
          v-hasPermi="['points:item:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['points:item:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['points:item:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['points:item:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="itemList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="id" align="center" prop="id" />
      <el-table-column label="优惠券名称" align="center" prop="name" />
      <el-table-column label="优惠券面值" align="center" prop="price" />
      <el-table-column label="领取方式" align="center" prop="pointsType" width="180">
        <template slot-scope="scope">
          <span>{{ scope.row.pointsType===1?"用户领取":scope.row.pointsType===2?"新用户自动发放":scope.row.pointsType===3?"系统赠送":scope.row.pointsType===4?"付费会员专享":"未知" }}</span>
        </template>
      </el-table-column>
      <el-table-column label="领取日期" align="center" prop="getTimeType" width="180">
        <template slot-scope="scope">

            <span>{{ scope.row.getTimeType==2?"不限时":parseTime(scope.row.getTimeStart, '{y}-{m}-{d}')+"-"+scope.row.getTimeEnd }}</span>

        </template>


      </el-table-column>
      <el-table-column label="优惠券类型" align="center" prop="pointsType" width="180">
        <template slot-scope="scope">
          <span>{{ scope.row.pointsType==1?"通用券":scope.row.pointsType==2?"品类券":scope.row.pointsType==3?"商品券":"未知" }}</span>
        </template>
      </el-table-column>
      <el-table-column label="使用时间" align="center" prop="useType" width="180">
        <template slot-scope="scope">
          <span>{{ scope.row.useType==1?scope.row.useDayValue+"天":scope.row.useTimeStart+"-"+scope.row.useTimeEnd }}</span>
        </template>
      </el-table-column>
      <el-table-column label="发布数量" align="center" prop="hasNumValue" width="180">
        <template slot-scope="scope">
          <span>{{ scope.row.hasNumType==2?"不限量":scope.row.hasNumValue+"张" }}</span>
        </template>
      </el-table-column>

      <el-table-column label="状态开启关闭" align="center" prop="status" width="180">
        <template slot-scope="scope">
          <span>{{ scope.row.status==1?"开启":"关闭" }}</span>
        </template>
      </el-table-column>


      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['points:item:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleCtrlV(scope.row,'copy')"
            v-hasPermi="['points:item:edit']"
          >复制</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['points:item:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改优惠券页面对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="优惠券名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入优惠券名称" />
        </el-form-item>
        <el-form-item label="优惠券面值" prop="price">
          <el-input v-model="form.price" placeholder="请输入优惠券面值" />
        </el-form-item>
        <el-form-item label="类型的值，通用无值" prop="pointsValue">
          <el-input v-model="form.pointsValue" placeholder="请输入类型的值，通用无值" />
        </el-form-item>
        <el-form-item label="门槛的最低消费金额" prop="sillValue">
          <el-input v-model="form.sillValue" placeholder="请输入门槛的最低消费金额" />
        </el-form-item>
        <el-form-item label="天数的值" prop="useDayValue">
          <el-input v-model="form.useDayValue" placeholder="请输入天数的值" />
        </el-form-item>
        <el-form-item label="领取后的开始时间段" prop="useTimeStart">
          <el-date-picker clearable
                          v-model="form.useTimeStart"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择领取后的开始时间段">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="领取后的结束时间段" prop="useTimeEnd">
          <el-date-picker clearable
                          v-model="form.useTimeEnd"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择领取后的结束时间段">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="显示领取的开始时间" prop="getTimeStart">
          <el-date-picker clearable
                          v-model="form.getTimeStart"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择显示领取的开始时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="限时领取的结束时间" prop="getTimeEnd">
          <el-date-picker clearable
                          v-model="form.getTimeEnd"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择限时领取的结束时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="限量数量" prop="hasNumValue">
          <el-input v-model="form.hasNumValue" placeholder="请输入限量数量" />
        </el-form-item>
        <el-form-item label="用户可以领取的张数" prop="canGetNum">
          <el-input v-model="form.canGetNum" placeholder="请输入用户可以领取的张数" />
        </el-form-item>
        <el-form-item label="逻辑删除" prop="isdel">
          <el-input v-model="form.isdel" placeholder="请输入逻辑删除" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listItem, getItem, delItem, addItem, updateItem } from "@/api/points/item";
import {parseTime} from "../../../utils/ruoyi";

export default {
  name: "Item",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 优惠券页面表格数据
      itemList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        price: null,
        sendType: null,
        pointsType: null,
        pointsValue: null,
        sillType: null,
        sillValue: null,
        useType: null,
        useDayValue: null,
        useTimeStart: null,
        useTimeEnd: null,
        getTimeType: null,
        getTimeStart: null,
        getTimeEnd: null,
        hasNumType: null,
        hasNumValue: null,
        canGetNum: null,
        status: null,
        isdel: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    parseTime,
    /** 查询优惠券页面列表 */
    getList() {
      this.loading = true;
      listItem(this.queryParams).then(response => {
        this.itemList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        name: null,
        price: null,
        sendType: null,
        pointsType: null,
        pointsValue: null,
        sillType: null,
        sillValue: null,
        useType: null,
        useDayValue: null,
        useTimeStart: null,
        useTimeEnd: null,
        getTimeType: null,
        getTimeStart: null,
        getTimeEnd: null,
        hasNumType: null,
        hasNumValue: null,
        canGetNum: null,
        status: null,
        isdel: null
      };
      this.resetForm("form");
    },
    handleCtrlV(row,copy){
      this.reset();
      const id = row.id;
      this.$router.push({
        path: '/item/add',
        query: {
          id: id,
          copy: copy
        }
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd(form) {
      this.reset();
      this.$router.push({
        path:'/item/add',
        query:{
          form:form
        }
      })
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id;
      this.$router.push({
        path: '/item/add',
        query: {
          id: id
        }
      });
    },

    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateItem(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addItem(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除优惠券页面编号为"' + ids + '"的数据项？').then(function() {
        return delItem(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('points/item/export', {
        ...this.queryParams
      }, `item_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
